from django.db import models
import uuid
from .dimensionModel import Dimension
from .unitModel import Unit
from control.models.User import User


class Quantity(models.Model):
    uuid = models.UUIDField(primary_key=True, auto_created=True,
                            default=uuid.uuid4, editable=False, verbose_name="uuid")
    quantity_name = models.CharField(
        max_length=50, verbose_name="量名称", null=False, blank=True, default="")
    quantity_name_en = models.CharField(
        max_length=50, verbose_name="量英文名称", null=False, blank=True, default="")
    sei_id = models.CharField(
        max_length=200, verbose_name="量编码", null=False, blank=True, default="")
    quantity_symbol = models.CharField(
        max_length=50, verbose_name="量符号", null=False, blank=True, default="")
    quantity_abbreviation = models.CharField(
        max_length=50, verbose_name="量缩写", null=False, blank=True, default="")
    quantity_type = models.CharField(
        max_length=50, verbose_name="量类型", null=False, blank=False)
    dimension = models.ForeignKey(
        Dimension, verbose_name="量纲", on_delete=models.DO_NOTHING)
    quantity_description = models.TextField(
        verbose_name='量描述', null=False, blank=True, default="")
    quantity_description_en = models.TextField(
        verbose_name='量英文描述', null=False, blank=True, default="")
    quantity_comment = models.TextField(
        verbose_name='量备注', null=False, blank=True, default="")
    create_person = models.ForeignKey(
        User, verbose_name="创建人", related_name="create_quantity", on_delete=models.DO_NOTHING, null=False)
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    modify_person = models.ForeignKey(
        User, verbose_name="修改人", related_name="modify_quantity", on_delete=models.DO_NOTHING, null=False)
    modify_date = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    valid_flag = models.BooleanField(default=True, verbose_name="是否有效")

    class Meta:
        ordering = ['create_date']
        verbose_name = "Quantity"
        verbose_name_plural = "Quantities"

        constraints = [
            models.UniqueConstraint(fields=['quantity_name'], condition=models.Q(
                valid_flag=True) & ~models.Q(quantity_name=''), name='unique_quantity_name'),
            models.UniqueConstraint(fields=['quantity_name_en'], condition=models.Q(
                valid_flag=True) & ~models.Q(quantity_name_en=''), name='unique_valid_quantity_name_en'),
            models.UniqueConstraint(fields=['sei_id'], condition=models.Q(valid_flag=True) & ~models.Q(
                sei_id=''), name='unique_valid_quantity_sei_id'),
        ]

    def __str__(self):
        return "量" + "->" + self.quantity_name

    def show_full_name(self):
        if self.quantity_name != "" and self.quantity_name_en != "":
            return "{}({})".format(self.quantity_name, self.quantity_name_en)
        if self.quantity_name != "":
            return self.quantity_name
        if self.quantity_name_en != "":
            return self.quantity_name_en
        return "未命名量"

    def show_short_name(self):
        if self.quantity_name != "":
            return self.quantity_name
        if self.quantity_name_en != "":
            return self.quantity_name_en
        return "未命名量"


class QuantityUnitMap(models.Model):
    uuid = models.UUIDField(primary_key=True, auto_created=True,
                            default=uuid.uuid4, editable=False, verbose_name="uuid")
    quantity = models.ForeignKey(Quantity, on_delete=models.DO_NOTHING,
                                 verbose_name="量", related_name="quantity_unit_map_quantity")
    unit = models.ForeignKey(Unit, on_delete=models.DO_NOTHING,
                             verbose_name="单位", related_name="quantity_unit_map_unit")

    class Meta:
        verbose_name = "QuantityUnitMap"
        verbose_name_plural = "QuantityUnitMaps"

    def __str__(self):
        return self.quantity.quantity_name + " -> " + self.unit.unit_name + " : " + self.unit.symbol
